package com.njust.service.Impl;

import com.njust.common.ServerRespone;
import com.njust.dataobject.TrainParam;
import com.njust.dataobject.TrainParamExample;
import com.njust.form.ParamForm;
import com.njust.mapper.TrainParamMapper;
import com.njust.service.SysParamService;
import com.njust.vo.TrainParamVO.TrainParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zjwpro@foxmail.com
 * @date 2018/3/12 下午2:27
 */
@Service
@Slf4j
public class SysParamServiceImpl implements SysParamService{

    @Autowired
    private TrainParamMapper trainParamMapper;

    @Override
    public ServerRespone paramSave(ParamForm paramForm) {
        TrainParamExample trainParamExample=new TrainParamExample();

        //数据库的记录
        int count = trainParamMapper.countByExample(trainParamExample);

        if(count==0){
            TrainParam trainParam=new TrainParam();
            BeanUtils.copyProperties(paramForm,trainParam);
            trainParamMapper.insert(trainParam);
            return ServerRespone.createBySuccess("保存成功");
        }else {

            TrainParam trainParam = trainParamMapper.findlastItem();
            BeanUtils.copyProperties(paramForm,trainParam);
            trainParamMapper.updateByExample(trainParam,trainParamExample);
            return ServerRespone.createBySuccess("更新成功");
        }
    }

    @Override
    public ServerRespone showParam() {
        TrainParamVO trainParamVO=new TrainParamVO();

        //查出最后一条记录
        TrainParam trainParam = trainParamMapper.findlastItem();
        if(trainParam!=null){
            BeanUtils.copyProperties(trainParam,trainParamVO);
        }
        return ServerRespone.createBySuccess(trainParamVO);
    }
}
